<template>
  <div class="register">
    <div class="register-box">
      <el-form
        :model="registerForm"
        status-icon
        :rules="registerFormRules"
        ref="registerFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名字" prop="username">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            show-password
            v-model="registerForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            show-password
            v-model="registerForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="registerForm.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model.number="registerForm.age"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="poid">
          <!-- <el-select v-model="registerForm.poid" placeholder="请选择岗位">
            <el-option label="销售" value="销售"></el-option>
            <el-option label="程序员" value="程序员"></el-option>
          </el-select> -->
          <el-select v-model="registerForm.poid" placeholder="所有身份">
            <el-option
              v-for="item in post"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" required>
          <el-col :span="9">
            <el-form-item prop="birthday">
              <el-date-picker
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
                v-model="registerForm.birthday"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="权限" prop="limits">
          <el-select v-model="registerForm.limits" placeholder="请选择你的权限">
            <el-option label="普通员工" value="false"></el-option>
            <el-option label="管理人员" value="true"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="other">
          <el-input type="textarea" v-model="registerForm.other"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button type="danger" @click="toLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
var hash = require("object-hash");
export default {
  data() {
    var validatePass2 = (registerFormRules, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      post: "",
      // 搜索 选择的身份
      registerForm: {
        username: "",
        pass: "",
        checkPass: "",
        sex: "",
        age: "",
        poid: "",
        birthday: "",
        limits: "",
        other: "",
      },
      registerFormRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        age: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
        poid: [{ required: true, message: "请选择身份", trigger: "change" }],
        birthday: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        limits: [
          { required: true, message: "请选择你的身份", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getpost();
  },
  methods: {
    // 请求岗位数据
    getpost() {
      this.axios
        .get("/getpost")
        .then((response) => {
          // console.log(response);
          this.post = response.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    // 提交表单
    submitForm() {
      this.$refs.registerFormRef.validate(async (valid) => {
        if (valid) {
          // this.$message.success("注册成功");

          this.registerForm.pass = hash(this.registerForm.pass, {
            algorithm: "md5",
            encoding: "base64",
          });
          this.registerForm.checkPass = hash(this.registerForm.checkPass, {
            algorithm: "md5",
            encoding: "base64",
          });
          // console.log("注册md5" + this.registerForm.password);
          const { data: res } = await this.axios.post(
            "/register",
            this.registerForm
          );
          if (res.code !== 200) {
            return this.$message.error("注册失败");
          }
          this.$message.success("注册成功,即将跳转到登录界面");
          setTimeout(() => {
            this.$router.push("/login");
          }, 2000);
        } else {
          this.$message.error("验证没有通过");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.registerFormRef.resetFields();
    },
    toLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.register {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #5d6371;
}
.register-box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 500px;

  padding: 10px;
  background-color: #fff;
}
</style>